import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

public class Test {
	public static void main(String[] args) {
		
		   ListOfTypes L1 = new ListOfTypes(10) ;
		   ListOfPredicates L2 = new ListOfPredicates(20);
		   ListOfOperator L3 = new ListOfOperator(5);
		   Predicate Pr = new Predicate();
		   ListOfAtoms L4Init = new ListOfAtoms(50);
		   ListOfAtoms L4Goal = new ListOfAtoms(5);
		   ListOfObjects L5 = new ListOfObjects(25);
		   //////////////////////////////////////////////////////////////////////
		   //I will read the domain file (mystery.pddl):
		   String s="" ,s1="" , s2;
		   try{
		   File f = new File("C:/Users/user1/workspace/Adil/bin/mystery.pddl");
		   FileInputStream fo = new FileInputStream(f);
		   DataInputStream os = new DataInputStream(fo); 
		   
	         while ((s = os.readLine()) != null) {
	        	 s1=s1+"\n "+s;
	         }
		   }catch(IOException ioe) {} 
		   s1=s1.replace("(", "( ");		s1=s1.replace(")", " )");
		   //I will split the file and save each word in the array (((((((((((( t )))))))))))).
	       String[] t = s1.split("\\s+");
	       for(int d=0 ; d<t.length ; d++){System.out.println(t[d]);} //this line to print element of the array
	       
	       ///////////////////////////////////////////////////////////////////////////
	       // Here I search in the array (t) for Types and add them in the listOfTypes  .
	       for(int i =0 ; i<t.length ; i++){
	       	  if(t[i].equals(":types")){
			     for(int p=++i ;!t[p].startsWith(":")&&!t[p].startsWith(";"); p++){
			    	 if(!t[p].startsWith("(")&&!t[p].startsWith(" ")&&!t[p].startsWith(")"))
					 L1.addTypeByName(t[p]);
			     }
			     break ;
	          }	
	       }
	       L1.display(); 
	       ///////////////////////////////////////////////////////////////////////
	       //Here I search in the array (t) for predicates and add them in the listOfPredicates  .
	       for(int d =0 ; d < t.length  ; d++){
	    	  if( t[d].equals(":predicates")){
				     for(int n=++d ;n<t.length; n++){
				    	 if(t[n].equals(")")&&t[n+1].equals(")"))
				    		 break;
				    	  if (t[n].equals("(")){
				    		    n++;
				    		    L2.addPredicate(t[n]);
				    		    for(int ss = n+1 ; !t[ss].equals(")") ; ss++){
				    		      if(t[ss].startsWith("?")){
				    		    	L2.findPredicateObj(t[n]).getLT().addTypeByName(t[ss]) ; 
				    		        for(int x=ss;!t[x].equals(")");x++){
				    		    	  if(t[x].equals("-")){
				    		    		  L2.findPredicateObj(t[n]).getLT().findType2(t[ss]).getT().setName(t[x+1]);
				    		    	      break; 
				    		    	  }
				    		        } 
				    		      }
				    		    }
				    	  }
				     }  
				     break ;
			  }    
	       }   	
	      L2.display();
	      ////////////////////////////////////////////////////////////////
	     //Here I will search in the array (t) for operators (actions) and add them in the ListOfOperators 
	     
	      String ss11 ;
	      for(int qq =0 ; qq < t.length  ; qq++){
	    	  if( t[qq].equals(":action")){
	    		  ++qq;    ss11=L3.addOperatorByName(t[qq]).getName();
	    		  for(int n1=++qq ;!t[n1].equals(")")&&!t[n1+1].equals(")")&&!t[n1+2].equals(")"); n1++){
	    			  if(t[n1].equals(":parameters")){
	    				  for(int t1t=++n1 ;!t[t1t].equals(":precondition");t1t++){
	    					  if(t[t1t].startsWith("?")){
	    						  L3.findOperator(ss11).getParameter().addTypeByName(t[t1t]);
	    						  for(int x=t1t;!t[x].equals(")");x++){
				    		    	  if(t[x].equals("-")){
				    		    		  L3.findOperator(ss11).getParameter().findType2(t[t1t]).getT().setName(t[x+1]);
	    					              break;
	    					      }}
	    				      }
	    			      }  
	    			  }}}}
	      L3.display();
	      ///////////////////////////////////////////////////////////////
	      //Here I will read the problem file (mystery_problem.pddl).
	      System.out.println("the problem file is : ");
	      String st1="" ,st2="" , st3;
		   try{
		   File f2 = new File("C:/Users/user1/workspace/Adil/bin/mystery_problem.pddl");
		   FileInputStream fo1 = new FileInputStream(f2);
		   DataInputStream os1 = new DataInputStream(fo1); 
		   
	         while ((st1 = os1.readLine()) != null) {
	        	 st2=st2+"\n "+st1;
	         }
		   }catch(IOException ioe) {} 
		   st2=st2.replace("(", "( ");		st2=st2.replace(")", " )");	
	       String[] tt = st2.split("\\s+"); // Here I will split the file and save each word in array of string ((((tt)))).
	       for(int d=0 ; d<tt.length ; d++){System.out.println(tt[d]);} // print element of the array.
	      ////////////////////////////////////////////////////////////////
	       //Here I will search in the array (tt) for the objects and add them in the ListOfObject
	       for(int d =0 ; d < tt.length  ; d++){
		    	  if( tt[d].equals(":objects")){
					     for(int n=++d ;n<tt.length; n++){
					    	 if(tt[n].equals(")"))
					    		 break;
					    	  if (!tt[n].equals("-")&&!tt[n-1].equals("-")){
					    		  L5.addObject(tt[n]);
					    		  for(int x=n;!tt[x].equals(")");x++){
				    		    	  if(tt[x].equals("-")){
				    		    		  L5.findObject(tt[n]).getT().setName(tt[x+1]);
				    		    	      break; 
				    		    	  }
					    	      }
					     }  
					     
				  } 
					     break ;
		       }
	       }
	       L5.display();
	      /////////////////////////////////////////////////////////
	       //Here I will search in the array (tt) for the init and add them in the ListOfAtoms
	       System.out.println("===============list of init=====================");
	     int c1=0;
	      for(int i=0 ; i<tt.length ; i++){
	    	  if(tt[i].equals(":init")){
	    		  for(int ii=++i ; !tt[ii].equals(")")||!tt[ii+1].equals(")") ; ii++){
	    			  if(tt[ii].equals("(")){
	    				  ii++;
	    				    L4Init.addAtom(tt[ii]);
	    				    for(int nn=ii+1 ; !tt[nn].equals(")") ; nn++){
	    				    	L4Init.getAtom()[c1].getLO().addObject2(L5.findObject(tt[nn]));
	    				    }
	    				    c1++;
	    			  }
	    		  }
	    	  }
	      }
	      L4Init.display();
	     //////////////////////////////////////////////////////////////////////////
	      //Here I will search in the array ( tt ) for goal and add them in another ListOfAtoms
	      int c2=0;
	      System.out.println("===============list of goal=======================");
	      for(int i=0 ; i<tt.length ; i++){
	    	  if(tt[i].equals("and")&&tt[i-2].equals(":goal")){
	    		  for(int ii=++i ; !tt[ii].equals(")")||!tt[ii+1].equals(")") ; ii++){
	    			  if(tt[ii].equals("(")){
	    				  ii++;
	    				  L4Goal.addAtom(tt[ii]);
	    				  for(int nn=ii+1 ; !tt[nn].equals(")") ; nn++){
	    					  L4Goal.getAtom()[c2].getLO().addObject2(L5.findObject(tt[nn]));
	    				    }
	    				    c2++;
	    			  }
	    		  }
	    		  
	    	  }
	    	  
	      }
	      L4Goal.display();
	      ///////////////////////////////////////////////////////////////////////
	}

}